import javax.swing.*;
import java.util.HashMap;

/**
 * This class defines an HTML table-data (td) element,
 * which represents as individual cell in the table.
 * 
 * @author Dennis Mitchell 
 * @version 1.001 09/09/2010
 */
public class HtmlTableCell
{
    //holds the cell text data
    private String data;
    
    //default size for JFrame
    private static final int FRAME_WIDTH=500;
    private static final int FRAME_HEIGHT=500;
    
    //valid ranges for css properties
    private static final int WIDTH_MIN=1;
    private static final int WIDTH_MAX=1000;
    private static final int HEIGHT_MIN=1;
    private static final int HEIGHT_MAX=1000;
    private static final int FONT_SIZE_MIN=1;
    private static final int FONT_SIZE_MAX=200;
    
    //holds each property-value declaration
    //for css styles.
    private HashMap<String,String> style;

    /**
     * Default constructor for class.  Initializes the
     * style HashMap object.
     */
    public HtmlTableCell()
    {
        style = new HashMap<String,String>();
    }


    /**
     * Sets the text (data) for the cell
     */
    public void setData(String data)
    {
        this.data = data;
    }
    
    /**
     * Gets the text (data) for the cell
     * @return String representing cell's data
     */
    public String getData()
    {
        return data;
    }
    
  
    /**
     * Sets the width (horizontal size) for a table-data element
     * @param cellWidth the width of the cell in pixels
     * (between 1 and 1000)
     */
    public void setWidth(Integer cellWidth)
    {
        //if width is in proper range, add to style HashMap
        if (cellWidth >= WIDTH_MIN && cellWidth <= WIDTH_MAX)
            style.put("width",cellWidth.toString() + "px");
        else
            style.remove("width"); //otherwise remove (if present)
    }


    /**
     * Sets the height (vertical size) for the cell
     * @param cellHeight the height of the cell in pixels
     * (between 1 and 1000)
     */
    public void setHeight(Integer cellHeight)
    {
        //if height is in proper range, add to style HashMap
        if (cellHeight >= HEIGHT_MIN && cellHeight <= HEIGHT_MAX)
            style.put("height",cellHeight.toString() + "px");
        else
            style.remove("height"); //otherwise remove (if present)
    }
    

    /**
     * Sets the size of the font for the cell 
     * @param fontSize the size of the font in pixels
     * (between 1 and 200)
     */
    public void setFontSize(Integer fontSize)
    {
        //if font size is in proper range, add to style HashMap
        if (fontSize > FONT_SIZE_MIN && fontSize <= FONT_SIZE_MAX)
            style.put("font-size",fontSize.toString() + "px");
        else
            style.remove("font-size"); //otherwise remove (if present)
    }

    
    /**
     * Sets the text alignment for the cell 
     * @param textAlignment left, center, or right
     */
    public void setTextAlignment(String textAlignment)
    {
        //if text alignment is one of the valid options, add to style HashMap
        if (textAlignment == "left" || textAlignment == "center" || textAlignment == "right")
            style.put("text-align",textAlignment);
        else
            style.remove("text-align"); //otherwise remove (if present)
    }

    /**
     * Sets the color of the font for the cell
     * @param fontColor valid RgbColor color
     */
    public void setFontColor(RgbColor fontColor)
    {
        //if fontColor is not null, add to style HashMap
        if (fontColor !=null)
            style.put("color",fontColor.getHtml());
        else
            style.remove("color"); //otherwise remove (if present)
    }
    
    /**
     * Sets the color of the background for the cell
     * @param backgroundColor valid RgbColor color
     */
    public void setBackgroundColor(RgbColor backgroundColor)
    {
        //if backgroundColor is not null, add to style HashMap
        if (backgroundColor !=null)
            style.put("background-color",backgroundColor.getHtml());
        else
            style.remove("background-color"); //otherwise remove (if present)
    }
    

    /**
     * Gets the HTML for the cell (table-data element), 
     * including inline style declarations, if present
     * @return String holding the td element and style desclarations
     */
    public String getHtml()
    {
     
        //instantiate new StringBuilder
        StringBuilder html = new StringBuilder();
        
        //create start element for table data
        html.append("<td");
        
        if (!style.isEmpty())
        {
            html.append(" style='");
            
            for(String s : style.keySet())
                html.append(" " + s + ":" + style.get(s) + ";");
                
            html.append("'");
        }
        
        //end the table data start element
        html.append(">");
        
        //if data are present, add it; otherwise, include nonbreaking space entity
        if (data != null && data != "")
            html.append(data);
        else
            html.append("&nbsp;");
                    
        //append end element for table data
        html.append("</td>");
        
        //return the StringBuilder's String
        return html.toString();
        
    }
    
    /**
     *  Wraps the cell in a single-row table and displays the cell
     *  in a JLabel within a JFrame
     */
    public void showInJFrame()
    {
            //instantiate a new JFrame with caption for month and year
            JFrame frame = new JFrame("Table Cell");
            
            //build the HTML for the table-data element
            String html = "<html><body><table><tr>" + getHtml() + "</tr></table></body></html>";
            
            
            //instantiate a new JLabel, whose contents are the HTML calendar,
            //and center the calendar JLabel
            JLabel htmlTable = new JLabel(html, SwingConstants.CENTER);
            
            //add the calendar JLabel to the JFrame
            frame.getContentPane().add(htmlTable);
            
            //set the JFrame's size
            frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
            
            //make the JFrame visible
            frame.setVisible(true);        
        
        
    }    
    
}
